/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.jbfilter.bean.FilterBean;
import org.jbfilter.bean.FilterComponentBean;
import org.jbfilter.bean.FilterHandlerBean;
import org.jbfilter.core.FilterComponent;

class _FilterBeanImpl<E> extends _FilterImpl<E> implements FilterBean<E> {

	public _FilterBeanImpl() {
		super();
		addClassRestriction(FilterComponentBean.class);
	}
	@Override
	public List<E> filter(Collection<E> toFilter) {
		// for performance reasons due to heavy use of remove operations we create a linked list
		LinkedList<E> retour = new LinkedList<E>(toFilter);
		for (FilterComponent<E> fc : getFilterComponents()) {
			((FilterComponentBean<E>)fc).filterOut(retour);
		}
		return retour;
	}
	@Override
	public List<E> filter(E[] toFilter) {
		return filter(Arrays.asList(toFilter));
	}

	@Override
	public boolean pass(E bean) {
		for (FilterComponent<E> fc : getFilterComponents()) {
			if(!((FilterComponentBean<E>)fc).pass(bean)) {
				return false;
			}
		}
		return true;
	}
	@Override
	public FilterHandlerBean<E> newFilterHandler() {
		return new FilterHandlerBean<E>(this);
	}
}
